<route lang="json5" type="page">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '套餐设置',
  },
}
</route>

<template>
  <view style="height: 100vh; margin-top: 30rpx">
    <wd-card
      custom-title-class="card_cutsom"
      custom-class="m-0 p-0"
      :custom-footer-class="wd - card__footer"
    >
      <template #title>
        <view
          style="
            display: flex;
            align-items: center;
            width: 100%;
            justify-content: space-between;
            padding: 30rpx 0;
          "
        >
          <text style="flex: 1">支付套餐</text>
          <wd-button @click="addPackage" size="small" style="flex: 1">添加套餐</wd-button>
        </view>
      </template>

      <wd-cell
        :border="true"
        :title="`游戏币：${item.gameCount}` + `（${item.packagePrice / 100}元）`"
        custom-class="cust_cell"
        v-for="(item, index) in data"
        :key="item.id"
      >
        <view>
          <wd-button size="small" @click="editPackage(item.id)">编辑</wd-button>
          <wd-button custom-class="ml-2" type="error" size="small" @click="deletePackage(item.id)">
            删除
          </wd-button>
        </view>
      </wd-cell>
    </wd-card>
  </view>
</template>

<script lang="ts" setup>
import { http, httpPost } from '@/utils/http'
import { onShow } from '@dcloudio/uni-app'
// 获取列表
const { loading, error, data, run } = useRequest(
  async () => await httpPost('/api/app/order/package/list'),
)

// onshow
onShow(() => {
  run()
})

const deletePackage = async (id) => {
  try {
    uni.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: async (res) => {
        if (res.confirm) {
          uni.showLoading({
            title: '删除中',
            mask: true,
          })

          await httpPost('/api/app/order/package/delete', { ids: [id] })
          uni.showToast({
            title: '删除成功',
            icon: 'none',
          })

          uni.hideLoading()

          run()
        }
      },
    })
  } catch (error) {}
}

const editPackage = async (id) => {
  // 跳到编辑也
  uni.navigateTo({
    url: '/pages/index/package/edit' + '?id=' + id,
  })
}

const addPackage = async (id) => {
  uni.navigateTo({
    url: '/pages/index/package/edit',
  })
}
</script>

<style lang="scss" scoped>
//
.text {
  margin-top: 12px;
}

:deep(.card_cutsom) {
  padding: 0 !important;
}

:deep(.wd-card__footer) {
  //   display: none;
}

:deep(.cust_cell) {
  padding: 0 !important;
}

:deep(.wd-cell__wrapper) {
  padding-right: 0px !important;
}
</style>
